﻿using System;
using System.Windows;
using System.Linq;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.Prompt;
using Baosight.Platform.Core.ServiceModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Baosight.ZNH.ZMCL.UI.PopWindows
{
    public partial class RiskRecordHandle : EFViewCmes
    {
        public RiskRecordHandle(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }
       
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    this.fdRiskDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    this.fdRiskDate.IsEnabled = false;
                    this.fdSourceId.IsEnabled = false;
                    this.fdWorknumId.IsEnabled = false;
                    this.fdDepartment.IsReadOnly = true;
                    this.fdManageDepartment.IsReadOnly = true;
                    this.fdResponsibilityPeople.IsReadOnly = true;
                    this.fdRiskPlace.IsReadOnly = true;
                    this.fdRiskContent.IsReadOnly = true;
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            string curDate = fdRiskDate.GetDataValue().ToString();
            if (fdDeadlineDate.Value != null)//判断解决日期是否输入合理
            {
                if (!fdDeadlineDate.Value.ToString().Equals(""))
                {
                    if (curDate.CompareTo(fdDeadlineDate.GetDataValue().ToString()) > 0)
                    {
                        Prompts pop = Prompts.getInstance();
                        pop.SetMsg("解决期限不能安排在隐患日期之前！");
                        pop.Show(6);
                        return;
                    }
                }
            }
            base.OnCommitButtonClick("ZMCL70", "handle", sender, e);//执行操作
        }

        /// <summary>
        /// 重写了确定按钮的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;

            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //如果勾选了需要罚款，则构造罚款页面
                if ("1".Equals(this.isPenalty.Value.ToString())) 
                {
                    EIRow eIRow = inInfo.GetBlock("result").GetRow(0);
                    RiskPenalty _penalty = new RiskPenalty("insert");
                    
                    _penalty.fdSendnoteDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    _penalty.fdRiskRecordId.Value = eIRow["fdKeyId"];//隐患记录ID
                    _penalty.fdSendnoteDepartment.Value = eIRow["fdManageDepartment"];//安全主管部门
                    _penalty.fdRiskHandleId.Value = inInfo.Get("fdRiskHandleId");//隐患处理ID
                    _penalty.fdDepartmentId.Value = eIRow["fdDepartmentId"];//责任部门ID
                    _penalty.fdDepartmentName.Value = eIRow["fdDepartment"];//责任部门
                    
                    _penalty.EIDataContext = new Platform.Core.UI.EIDataContext();
                    _penalty.EIDataContext.Update(inInfo);

                    Collection<EIRow> penaltyType = inInfo.GetBlock("cmes.cl.penaltyType").GetRows();
                    IEnumerable<EIRow> penaltyTypeResult = from c in penaltyType where c["display"].ToString().IndexOf("现金") >= 0 select c;
                    if (penaltyTypeResult.Count() > 0)
                    {
                        _penalty.fdClassify.Value = penaltyTypeResult.First()["value"].ToString();
                    }

                    EFWindow _popWindow = this.Parent as EFWindow;
                    _popWindow.DialogResult = true;
                    _popWindow.Content = _penalty;
                    _popWindow.Header = "隐患处理-罚款";
                }
                //不勾选需要罚款，则直接关闭窗口
                else if ("0".Equals(this.isPenalty.Value.ToString()))
                {
                    EFWindow window = this.Parent as EFWindow;
                    window.DialogResult = true;
                    window.Close();
                }
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }
    }
}

